import { areaList } from '@vant/area-data';
var http = require("../../../utils/https.js")
Page({

    /**
     * 页面的初始数据
     */
    data: {
        areaList, // 地址组件
        address: '',
        show: false,
        name: '',
        phone: '',
        details: '',
        myAddress: {
            city: "",
            cityCode: "",
            createBy: null,
            createTime: null,
            del: 0,
            details: "",
            district: "",
            districtCode: "",
            id: "",
            isDefault: 0,
            province: "",
            provinceCode: "",
            receiverName: null,
            receiverPhone: null,
            remark: null,
            updateBy: null,
            updateTime: null,
            userId: null,
        },

    },

    onChange({ detail }) {
        var isDefault = detail == true ? 1 : 0
        this.setData({
            "myAddress.isDefault": isDefault
        });
        console.log(detail);
        console.log(this.data.myAddress.isDefault);
    },
    showPopup() {
        this.setData({ show: true });
    },
    // 确认选择地址
    onConfirm(event) {
        var text = event.detail.values; // 获取选中的地址信息
        console.log(text);
        var addres = this.data.myAddress
        addres.provinceCode = text[0].code
        addres.province = text[0].name
        addres.cityCode = text[1].code
        addres.city = text[1].name
        addres.district = text[2].name
        addres.districtCode = text[2].code
        this.setData({
            myAddress: addres
        })
        var address = ""
        address = addres.province + "," + addres.city + "," + addres.district
        this.setData({
            address: address
        })
        console.log(this.data.myAddress);
        this.setData({ show: false }); // 关闭弹窗
    },

    onClose() {
        this.setData({ show: false });
    },
    yes() {
        var name = this.data.name
        var phone = this.data.phone
        var details = this.data.details
        var myaddress = this.data.myAddress
        myaddress.details = details
        myaddress.receiverPhone = phone
        myaddress.receiverName = name
        this.setData({
            myAddress: myaddress
        })
        console.log(this.data.myAddress);
        http.PUT('/user/address', myaddress, function (res) {
            if (res.code === 200) {
               wx.reLaunch({
                 url: '/my/pages/mzhAddress/mzhAddress',
               })
            }
        }, function (err) {
            console.error('失败:', err);
        });




    },

    onLoad(options) {
        var id = options.id
        var that = this
        http.GET(`/user/address/${id}`, {}, function (res) {
            if (res.code === 200) {
                that.setData({
                    myAddress: res.data,
                })
                var myAddress = that.data.myAddress
                var address = ""
                address = myAddress.province + "," + myAddress.city + "," + myAddress.district
                that.setData({
                    address: address,
                    name: myAddress.receiverName,
                    phone: myAddress.receiverPhone,
                    details: myAddress.details,

                })

                console.log('成功:', res);
            } else {
                wx.showToast({
                    title: '登录过期',
                    duration: 1000
                })

                setTimeout(() => {
                    wx.redirectTo({
                        url: '/index/pages/mzhLogin/mzhLogin',
                    })
                }, 1500);
            }

        }, function (err) {
            console.error('失败:', err);
        });
    },
})